﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using GSM.BusinessLayer;
using GSM.BusinessLayer.DataLayer;
using CRM_SMS_MARKETING.EL;
using CRM_SMS_MARKETING.BL;
using MD5;

namespace CRM_SMS_MARKETING.PL.Customer_Care
{
    public partial class frmBirthday : DevExpress.XtraEditors.XtraForm
    {
        #region Attribute
        BLEvent blEvent;
        EventSql evensql;
        PromotionSql promotionsql;
        frmBirthdayCustomer frmBirhthDayCustomer;
        List<ELNewPromotion> ListVoucher;
        List<ELNewPromotion> ListGift;
        string smsContent;
        string customername;
        string companyName;
        string companyAddress;
        string dateSubject;
        string dateStart;
        string dateEnd;
        string gift;
        string voucher;
        #endregion
        public static List<GSM.BusinessLayer.Customer> ListCustomer;
        public frmBirthday()
        {
            InitializeComponent();
            blEvent = new BLEvent();
            checkEditStatus.Properties.ReadOnly = true;
            txtDescription.Properties.ReadOnly = true;
            txtSMSFormat.Properties.ReadOnly = true;
            txtExtraTime.Properties.ReadOnly = true;
            List<SmallPromotion> Voucher = new List<SmallPromotion>();
            List<SmallPromotion> Gift = new List<SmallPromotion>();
            ListVoucher = new List<ELNewPromotion>();
            evensql = new EventSql();
            ListCustomer = new List<GSM.BusinessLayer.Customer>();
            ListGift = new List<ELNewPromotion>();

            promotionsql = new PromotionSql();
            //Lookup Edit
            Voucher = promotionsql.SelectAllSmallPromotionVoucher();
            Gift = promotionsql.SelectAllSmallPromotionGift();

            //Tao gia tri rong cho lookup edit
            blEvent.CreateListVoucherAndGift(Voucher, Gift, ListVoucher, ListGift);
            lookUpEditVoucher.Properties.DataSource = ListVoucher;
            lookUpEditVoucher.Properties.DisplayMember = "PromotionSubject";
            lookUpEditVoucher.Properties.ValueMember = "PromotionSubject";


            lookUpEditGift.Properties.DataSource = ListGift;
            lookUpEditGift.Properties.DisplayMember = "PromotionSubject";
            lookUpEditGift.Properties.ValueMember = "PromotionSubject";

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {


            Event even = new Event();
            even = blEvent.GetEventByID(1);
            #region Mapping field
            checkEditStatus.EditValue = even.EventStatus;
            if (even.EventStatus == true)
            {
                labelStatus.Text = "This event is now enable";
            }
            else
            {
                labelStatus.Text = "This event is now disable";
            }


            txtDescription.Text = even.Description;
            txtSMSFormat.Text = even.Content;
            #region Lay thong tin promotion
            Promotion proVoucher = new Promotion();
            Promotion proGift = new Promotion();
            proVoucher = blEvent.GetPromotionByID(even.PromotionID.ToString());
            proGift = blEvent.GetPromotionByID(even.PromotionID2.ToString());
            lookUpEditVoucher.EditValue = proVoucher.PromotionSubject;
            lookUpEditGift.EditValue = proGift.PromotionSubject;
            txtVoucherDescription.Text = proVoucher.PromotionContent;
            txtGiftDescription.Text = proGift.PromotionContent;
            #endregion
            //Su dung cot FormatSMS1 de luu extra time
            //Lam nhu the nay de khong phai tao them bang Birthday ma su dung lai bang event
            txtExtraTime.Text = even.FormatSMS1;

            #endregion
            //Su dung cot FormatSMS1 de luu extra time
            //Lam nhu the nay de khong phai tao them bang Birthday ma su dung lai bang event

            #region Set ReadOnly
            checkEditStatus.Properties.ReadOnly = true;
            txtDescription.Properties.ReadOnly = true;
            txtSMSFormat.Properties.ReadOnly = true;
            txtExtraTime.Properties.ReadOnly = true;
            txtGiftDescription.Properties.ReadOnly = true;
            txtVoucherDescription.Properties.ReadOnly = true;
            lookUpEditGift.Properties.ReadOnly = true;
            lookUpEditVoucher.Properties.ReadOnly = true;
            btnFilter.Enabled = false;
            comboBoxEditField.Properties.ReadOnly = true;
            btnEdit.Enabled = true;
            btnSave.Enabled = false;
            btnCancel.Enabled = false;
            btnInsertField.Enabled = false;
            #endregion
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            checkEditStatus.Properties.ReadOnly = false;
            txtDescription.Properties.ReadOnly = false;
            txtSMSFormat.Properties.ReadOnly = false;
            txtExtraTime.Properties.ReadOnly = false;
            txtGiftDescription.Properties.ReadOnly = false;
            txtVoucherDescription.Properties.ReadOnly = false;
            lookUpEditGift.Properties.ReadOnly = false;
            lookUpEditVoucher.Properties.ReadOnly = false;
            btnFilter.Enabled = true;
            comboBoxEditField.Properties.ReadOnly = false;
            btnEdit.Enabled = false;
            btnSave.Enabled = true;
            btnCancel.Enabled = true;
            btnInsertField.Enabled = true;
        }

        private void frmBirthday_Load(object sender, EventArgs e)
        {
            Event even = new Event();
            even = blEvent.GetEventByID(1);
            ListCustomer = blEvent.GetListCustomerOfEvent(1);
            #region Mapping field
            checkEditStatus.EditValue = even.EventStatus;
            if (even.EventStatus == true)
            {
                labelStatus.Text = "This event is now enable";
            }
            else
            {
                labelStatus.Text = "This event is now disable";
            }


            txtDescription.Text = even.Description;
            txtSMSFormat.Text = even.Content;
            #region Lay thong tin promotion
            Promotion proVoucher = new Promotion();
            Promotion proGift = new Promotion();
            proVoucher = blEvent.GetPromotionByID(even.PromotionID.ToString());
            proGift = blEvent.GetPromotionByID(even.PromotionID2.ToString());
            lookUpEditVoucher.EditValue = proVoucher.PromotionSubject;
            lookUpEditGift.EditValue = proGift.PromotionSubject;
            txtVoucherDescription.Text = proVoucher.PromotionContent;
            txtGiftDescription.Text = proGift.PromotionContent;
            #endregion
            //Su dung cot FormatSMS1 de luu extra time
            //Lam nhu the nay de khong phai tao them bang Birthday ma su dung lai bang event
            txtExtraTime.Text = even.FormatSMS1;

            #endregion
        }
        public string ParseSMS(string smsFormat)
        {
            //string smsContent = "";
            string customername = "Linh Nguyễn";
            string companyName = "Hoa Sen";
            string companyAddress = "Quan 12 TP HCM";
            string dateSubject = "Chúc mừng sinh nhật";
            string dateStart = "20-10";
            string dateEnd = "8-3";
            string gift = "Quà tặng";
            string voucher = "Voucher Giảm Giá";
            char[] smsArray = new char[1100];
            smsArray = smsFormat.ToCharArray();

            string result = "";
            int n = smsArray.Length;
            int i = 0;
            while (i < n)
            {
                if (smsArray[i].Equals('{'))
                {
                    #region CustomerName, Company
                    if (smsArray[i + 1] == 'C')
                    {
                        if (smsArray[i + 8] == 'r')
                        {
                            //CustomerName
                            result = result + customername;
                            i = i + 13;
                        }
                        else
                            if (smsArray[i + 8] == 'N')
                            {
                                //CompanyName
                                result = result + companyName;
                                i = i + 12;
                            }
                            else
                                if (smsArray[i + 8] == 'A')
                                {
                                    //CompanyAddress
                                    result = result + companyAddress;
                                    i = i + 15;
                                }
                    }
                    #endregion
                    #region Date
                    else
                        if (smsArray[i + 1] == 'D')
                        {
                            if (smsArray[i + 6] == 'u')
                            {
                                //DateSubject
                                result = result + dateSubject;
                                i = i + 12;
                            }
                            else
                                if (smsArray[i + 6] == 't')
                                {
                                    //DateStart
                                    result = result + dateStart;
                                    i = i + 10;
                                }
                                else
                                    if (smsArray[i + 6] == 'n')
                                    {
                                        //DateEnd
                                        result = result + dateEnd;
                                        i = i + 9;
                                    }
                        }
                    #endregion
                        #region Gift Voucher
                        else
                            if (smsArray[i + 1] == 'G')
                            {
                                //Gift Subject
                                result = result + gift;
                                i = i + 5;
                            }
                            else
                                if (smsArray[i + 1] == 'V')
                                {
                                    //Voucher Subject
                                    result = result + voucher;
                                    i = i + 8;
                                }

                        #endregion
                }
                else
                {
                    result = result + smsArray[i];
                }

                i++;

            }

            return result;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            Event even = new Event();
            bool checkupdate = true;
            try
            {

                lookUpEditVoucher.Properties.ValueMember = "ID";
                lookUpEditGift.Properties.ValueMember = "ID";
                #region Mapping
                even.EventID = 1;
                even.EventStatus = (bool)checkEditStatus.EditValue;
                even.Description = txtDescription.Text;
                even.Content = txtSMSFormat.Text;
                if (lookUpEditVoucher.EditValue.ToString() != "")
                {
                    even.PromotionID = Int32.Parse(lookUpEditVoucher.EditValue.ToString());
                               
                }
                else
                {
                    even.PromotionID = 1;
                }
                even.FormatSMS1 = txtExtraTime.Text;
                if (lookUpEditGift.EditValue.ToString() != "")
                {
                    even.PromotionID2 = Int32.Parse(lookUpEditGift.EditValue.ToString());
                         


                }
                else
                {
                    even.PromotionID2 = 2;
                }
                lookUpEditVoucher.Properties.ValueMember = "PromotionSubject";
                lookUpEditGift.Properties.ValueMember = "PromotionSubject";
    
                #endregion
                evensql.Update(even);
                if (ListCustomer.Count == 0)
                {
                    //Xoa tat ca cac Customer thuoc Event nay truoc day
                    blEvent.EvenCustomerDeleteByField("EventID", 1);
                    Customer_MessageSql cmsql = new Customer_MessageSql();
                    cmsql.DeleteByField("EventID", 1);
                }
                else
                {
                    blEvent.EvenCustomerDeleteByField("EventID", 1);
                    //Them moi toan bo customer duoc chon vao bang Event Customer
                    blEvent.EventCustomerInsertList(1, ListCustomer);
                    Customer_MessageSql cmsql = new Customer_MessageSql();
                    cmsql.DeleteByField("EventID", 1);
                    blEvent.CreateSMSForBirthday(even, lookUpEditVoucher.EditValue.ToString(), lookUpEditGift.EditValue.ToString(), ListCustomer, 1);


                }

            }
            catch (Exception ex)
            {
                checkupdate = false;
                MessageBox.Show(ex.Message);
            }
            if (checkupdate == true)
            {
                btnEdit.Enabled = true;
                MessageBox.Show("Edit Customer's Birthday Successfully");
                #region Set ReadOnly
                checkEditStatus.Properties.ReadOnly = true;
                txtDescription.Properties.ReadOnly = true;
                txtSMSFormat.Properties.ReadOnly = true;
                txtExtraTime.Properties.ReadOnly = true;
                txtGiftDescription.Properties.ReadOnly = true;
                txtVoucherDescription.Properties.ReadOnly = true;
                lookUpEditGift.Properties.ReadOnly = true;
                lookUpEditVoucher.Properties.ReadOnly = true;
                btnFilter.Enabled = false;
                comboBoxEditField.Properties.ReadOnly = true;
                btnEdit.Enabled = true;
                btnSave.Enabled = false;
                btnCancel.Enabled = false;
                btnInsertField.Enabled = false;
                #endregion

            }
            else
            {
                btnEdit.Enabled = false;
                MessageBox.Show("Edit Customer's Birthday fail");
            }



        }
        private void lookUpEditVoucher_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {

            lookUpEditVoucher.Properties.ValueMember = "ID";
            Promotion pro = new Promotion();
            pro = blEvent.GetPromotionByID(lookUpEditVoucher.EditValue.ToString());
            txtVoucherDescription.Text = pro.PromotionContent;
        }

        private void lookUpEditGift_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            lookUpEditGift.Properties.ValueMember = "ID";
            Promotion pro = new Promotion();
            pro = blEvent.GetPromotionByID(lookUpEditGift.EditValue.ToString());
            txtGiftDescription.Text = pro.PromotionContent;
        }

        private void checkEditStatus_CheckedChanged(object sender, EventArgs e)
        {
            if (checkEditStatus.Checked == true)
            {
                labelStatus.Text = "This event is now enable";
            }
            else
            {
                labelStatus.Text = "This event is now disable";
            }
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {

            //MessageBox.Show("{CustomerName} sẽ cố gắng cho công ty {CompanyName} chúng ta tại {CompanyAddress} có được {Voucher} và {Gift} và {DateSubject} và {DateStart} và {DateEnd}"+"\n"+ParseSMS("{CustomerName} sẽ cố gắng cho công ty {CompanyName} chúng ta tại {CompanyAddress} có được {Voucher} và {Gift} và {DateSubject} và {DateStart} và {DateEnd}"));
            if (frmBirhthDayCustomer == null)
            {
                frmBirhthDayCustomer = new frmBirthdayCustomer(this.Name, ListCustomer);
                frmBirhthDayCustomer.ShowDialog();
            }

            else
            {
                frmBirhthDayCustomer.ShowDialog();
            }
        }

        private void btnInsertField_Click(object sender, EventArgs e)
        {

            if (comboBoxEditField.Text == "Customer Name")
            {
                txtSMSFormat.Text = txtSMSFormat.Text + "{CustomerName}";//i+8=r
            }
            else
                if (comboBoxEditField.Text == "Company Name")
                {
                    txtSMSFormat.Text = txtSMSFormat.Text + "{CompanyName}";//i+8=n
                }
                else
                    if (comboBoxEditField.Text == "Company Address")
                    {
                        txtSMSFormat.Text = txtSMSFormat.Text + "{CompanyAddress}";//i+8=a
                    }
                    else
                        if (comboBoxEditField.Text == "Date Subject")
                        {
                            txtSMSFormat.Text = txtSMSFormat.Text + "{DateSubject}";//i+6=u
                        }
                        else
                            if (comboBoxEditField.Text == "Date Start")
                            {
                                txtSMSFormat.Text = txtSMSFormat.Text + "{DateStart}";//i+6=t
                            }
                            else
                                if (comboBoxEditField.Text == "Date End")
                                {
                                    txtSMSFormat.Text = txtSMSFormat.Text + "{DateEnd}";//i+6=n
                                }
                                else
                                    if (comboBoxEditField.Text == "Voucher")
                                    {
                                        txtSMSFormat.Text = txtSMSFormat.Text + "{Voucher}";
                                    }
                                    else
                                        if (comboBoxEditField.Text == "Gift")
                                        {
                                            txtSMSFormat.Text = txtSMSFormat.Text + "{Gift}";
                                        }
        }
    }
}